﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AlbumDesigner.Models.Enums;
using AlbumDesigner.Helpers;
using System.ComponentModel.DataAnnotations;

namespace Keepo.Models.Services
{
    static class PageTypes
    {
        public const string Internal = "internal";
        public const string SoftCover = "softCover";
        public const string HardCover = "hardCover";
    }

    static class SpineConstants
    {
        public const float HardSpineWidthInch = 0.3937f;
        public const float SoftSpineWidhtInch = 0.1575f;
    }

    public class AlbumPageExport
    {
        public AlbumPageExport(AlbumPage page)
        {
            this.IsPremade = false;

            this.AlbumPageId = page.AlbumPageId;
            this.AlbumSectionId = page.AlbumSectionId;
            this.AlbumSectionTitle = page.AlbumSection.Title;
            this.SectionPageIdx = page.SectionPageIdx;
            this.IsSingle = page.IsSingle;
            this.IsLeft = page.IsLeft;
            this.HasXml = page.HasXml;
            this.HasThumbnail = page.HasThumbnail;
            this.HasRender = page.HasRender;

            AlbumSize albumSize = page.AlbumSection.Album.AlbumSize;
            var isCover = false;

            // Page Type:
            if (page.AlbumSection.AlbumSectionTypeId == (int)AlbumSectionTypeEnum.Cover)
            {
                isCover = true;

                // Calculate spine width
                var album = page.AlbumSection.Album;
                var paper = album.Paper;
                var spineWidthMm = AlbumHelper.CalculateSpineWidthMm(album, paper);
                var spineWidthInch = AlbumPagesHelper.MMToInches(spineWidthMm);
                this.CoverSpineWidthInch = spineWidthInch;

                switch (page.AlbumSection.Album.AlbumSize.CoverTypeId)
                {
                    case (int)CoverTypeEnum.Soft:
                        this.PageType = PageTypes.SoftCover;
                        break;

                    case (int)CoverTypeEnum.Hard:
                        this.PageType = PageTypes.HardCover;
                        break;
                }
            }
            else
            {
                this.PageType = PageTypes.Internal;
            }

            this.AlbumSizeId = albumSize.AlbumSizeId;
            setPrintSize(albumSize, isCover);

            this.ThumbnailUrl = AlbumHelper.PageThumbnailUrl(page.AlbumSection.AlbumId, page.AlbumPageId) + "?mod=" + page.ModifiedDate.ToBinary();
        }

        public AlbumPageExport(PremadePage page)
        {
            this.IsPremade = true;

            // AlbumPage non relevant data
            this.AlbumPageId = -1;
            this.AlbumSectionId = -1;
            this.AlbumSectionTitle = "";
            this.SectionPageIdx = -1;
            this.HasRender = false;

            this.IsLeft = true;
            this.HasThumbnail = page.HasThumbnail;
            this.IsSingle = page.IsSingle;
            this.HasXml = page.HasXml;

            // TBD
            // this.CoverSpineWidthInch
            // this.PageType

            this.PremadePageId = page.PremadePageId;
            setPrintSize(page.AlbumSize, page.IsCover);

            this.AlbumSizeId = page.AlbumSizeId;
            this.Title = page.Title;
            this.CategoryId = page.PremadePageCategoryId;
            this.ThumbnailUrl = AlbumHelper.PremadePageThumbnailUrl(page.PremadePageId) + "?mod=" + page.ModifiedDate.ToBinary();
        }

        //parameterless constructor - neccesary for MVC ModelBind
        public AlbumPageExport()
        {

        }

        public void setPrintSize(AlbumSize albumSize, bool isCover)
        {
            var PageWidthMM = AlbumPagesHelper.MMToInches(albumSize.PageWidthMM);
            var PageHeightMM = AlbumPagesHelper.MMToInches(albumSize.PageHeightMM);
            var CoverBleedMM = AlbumPagesHelper.MMToInches(albumSize.CoverBleedMM);
            var PageBleedMM = AlbumPagesHelper.MMToInches(albumSize.PageBleedMM);
            var MinSpineMM = AlbumPagesHelper.MMToInches(albumSize.MinSpineMM);
            var MaxSpineMM = AlbumPagesHelper.MMToInches(albumSize.MaxSpineMM);
            var CartonThicknessMM = AlbumPagesHelper.MMToInches(albumSize.CartonThicknessMM);
            var BigSizeMM = AlbumPagesHelper.MMToInches(albumSize.BigSizeMM);

            if (isCover)
            {
                this.BleedOffsetXInch = CoverBleedMM + ((MaxSpineMM - MinSpineMM) / 2);
                this.BleedOffsetYInch = CoverBleedMM;

                this.BleedWidthInch = PageWidthMM + (2 * BigSizeMM) + (2 * CartonThicknessMM) + MinSpineMM;
                this.BleedHeightInch = PageHeightMM;

                this.PageWidthInch = PageWidthMM + (2 * CoverBleedMM) + (2 * BigSizeMM) + (2 * CartonThicknessMM) + MaxSpineMM;
                this.PageHeightInch = PageHeightMM + (2 * CoverBleedMM);
            }
            else
            {
                this.BleedOffsetXInch = PageBleedMM;
                this.BleedOffsetYInch = PageBleedMM;

                this.BleedWidthInch = PageWidthMM;
                this.BleedHeightInch = PageHeightMM;

                this.PageWidthInch = PageWidthMM + (2 * PageBleedMM);
                this.PageHeightInch = PageHeightMM + (2 * PageBleedMM);
            }
        }

        public int AlbumPageId { get; set; }
        public int AlbumSectionId { get; set; }
        public string AlbumSectionTitle { get; set; }
        public int SectionPageIdx { get; set; }
        public bool IsSingle { get; set; }
        public bool IsLeft { get; set; }

        // Calculated Page Number
        public int PageNum { get; set; }

        // Print Size and Bleed
        public float PageWidthInch { get; set; }
        public float PageHeightInch { get; set; }

        // Bleed 
        public float BleedOffsetXInch { get; set; }
        public float BleedOffsetYInch { get; set; }
        public float BleedWidthInch { get; set; }
        public float BleedHeightInch { get; set; }

        // Rendering        
        public bool HasXml { get; set; }
        public bool HasThumbnail { get; set; }
        public bool HasRender { get; set; }

        // Page Type
        public int PremadePageId { get; set; }
        public bool IsPremade { get; set; }
        public string PageType { get; set; }
        // Size of Spine (SHIDRA) case it's cover
        public float CoverSpineWidthInch { get; set; }

        [Required(ErrorMessage = "יש להזין כותרת")]
        public string Title { get; set; }

        public int AlbumSizeId { get; set; }
        public int CategoryId { get; set; }
        public string ThumbnailUrl { get; set; }
    }
}